@tool
class_name Skill_Control_Wait
extends SkillControl

## Wait 等待
## 等待一段时间后执行子Action，可以中途Stop
#region 属性
@export var wait_time_range:Vector2 = Vector2.ONE
## 是否等待子Action全部结束才算完成
@export var is_wait_for_all_sub_actions_finished:bool = false
var timer:CustomTimer
var finished_sub_action_count = 0
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
## 初始化
func init(action_idx:int = -1,init_data:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
	super.init(action_idx,init_data,self_handle_func)
## 释放
func release(args:Dictionary = {},handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		var wait_time = GameManager.get_grng().grng_randf_range(wait_time_range.x,wait_time_range.y)
		timer = GameCustomTimerSystem.create_timer(wait_time)
		connect_obj_dict[timer.timer_finished] = ConnectObj.new(timer.timer_finished,[on_timer_finished])
		finished_sub_action_count = 0
		timer.start_timer()
	super.release(args,self_handle_func)
## 打断
func interrupt(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		for action in sub_actions:
			action.interrupt()
	super.interrupt(self_handle_func)
## 完成
func complete(release_result:GameEnum.SkillReleaseResult = GameEnum.SkillReleaseResult.Success,handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		for action in sub_actions:
			action.complete()
	super.complete(release_result,self_handle_func)
## 冷却
func cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		for action in sub_actions:
			action.cool()
	super.cool(self_handle_func)
## 冷却完成
func cool_complete(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		for action in sub_actions:
			action.cool_complete()
	super.cool_complete(self_handle_func)
## 重置冷却
func reset_cool(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		for action in sub_actions:
			action.reset_cool()
	super.reset_cool(self_handle_func)
## 销毁
func destroy(handle_func:Callable = Callable()):
	var self_handle_func = func():
		if handle_func.is_valid():
			handle_func.call()
		if timer:
			GameCustomTimerSystem.remove_timer(timer.id)
			timer = null
		for action in sub_actions:
			action.destroy()
	super.destroy(self_handle_func)
## 判断该技能能否释放
func check_can_release() -> GameEnum.SkillReleaseStatus:
	var res = super.check_can_release()
	if res != GameEnum.SkillReleaseStatus.Success:
		return res
	return GameEnum.SkillReleaseStatus.Success
func on_sub_action_release_completed(skill_action:SkillAction,release_result:GameEnum.SkillReleaseResult):
	if not is_releasing:
		return
	finished_sub_action_count += 1
	if check_is_completed():
		complete(release_result)
func on_sub_action_interrupted(skill_action:SkillAction):
	if not is_releasing:
		return
	finished_sub_action_count += 1
	if check_is_completed():
		complete(GameEnum.SkillReleaseResult.Interrupted)
func check_is_completed() -> bool:
	return is_completed and (finished_sub_action_count >= sub_actions.size() if is_wait_for_all_sub_actions_finished else true)
func on_timer_finished(timer_result:CustomTimer.TimerResult):
	if timer_result == CustomTimer.TimerResult.Completed:
		for action in sub_actions:
			action.release()
		if not is_wait_for_all_sub_actions_finished:
			complete()
		else:
			is_completed = true
#endregion
#region 公共方法
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	if get_child_count() > 1:
		warnings.append("此节点最多只能有一个节点")
	return warnings
#endregion
